import numpy as np
import csv
import random
from datetime import datetime
from time import strftime


def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate


def multi_unique(inputList):
    str_list = np.array([])
    for item in inputList:
        temp_str = ""
        for x in item:
            temp_str += ";" + str(x)
        str_list = np.append(str_list, temp_str)
    #print str_list
    uni_str = np.unique(str_list)
    uni_float = np.array([])
    #print uni_str
    for item in uni_str:
        temp_item = item[1:]
        #print temp_item
        #break
        temp_list = temp_item.split(";")
        for temp_list_item in temp_list:
            uni_float = np.append(uni_float, float(temp_list_item))
    iLenInputList = inputList.shape
    uni_float.shape = (-1, iLenInputList[1])
    return uni_float

def build_data_list(inputCSV):
    sKey = np.array([])
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey =np.append(sKey, temp)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey


#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    
    #unitCSV = "C:/Documents and Settings/wang322/My Documents/Downloads/WelFlows_3.csv"
    unitCSV = "C:/_DATA/Flow/WelFlows_3_FlowCount.csv"
    dataMatrix = build_data_list(unitCSV)
    
    unitCSV = "C:/_DATA/Flow/Pt.csv"
    ptInfo = build_data_list(unitCSV)
    output = np.array([])
    
    for item in dataMatrix:
        output = np.append(output, ptInfo[int(item[0]), 1:3])
        output = np.append(output, ptInfo[int(item[1]), 1:3])
    output.shape = (-1,4)

    filePath = unitCSV[:-11] + "_temp.csv"
    np.savetxt(filePath, output, delimiter=',')  

    print "end at " + getCurTime()
    print "========================================================================"  

           
